<?php
$caches = array("./error_log", "cache/myDEBUG\\-.+\\.log", "bmz_cache/\\w");
$total=0;
for ($i=0; $i<count($caches); $i++){
	delfiles($caches[$i]);
}
echo "$total";
die();

function delfiles($regex) {
	$p = strrpos($regex, "/");
	$path = substr($regex, 0, $p);
	$regex = substr($regex, $p+1);
//	echo $path . '<br/>' . $regex . '<br/><br/>';
	dodel($path, '/'.$regex.'/');
}

function dodel($location='',$fileregex=''){
	if (!$location or !is_dir($location) or !$fileregex) {
		return false;
	}
	$all = opendir($location);
	while ($file = readdir($all)) {
		if ($file == ".." || $file == ".")
			continue;
		if (preg_match($fileregex,$file)){
			delfile($location.'/'.$file);
//			echo "$location/$file<br/>";
		}
	}
	closedir($all);
	unset($all);
}
function delfile($dir) {
	global $total;
	if (!file_exists($dir)){
		return true;
	}else{
		@chmod($dir, 0777);
	}
	if(!is_dir($dir)) {
		$total += filesize($dir);
		unlink($dir);
		return;
	}
	$dh=opendir($dir);
	while ($file=readdir($dh)) {
		if($file!="." && $file!="..") {
			$fullpath=$dir."/".$file;
			if(!is_dir($fullpath)) {
				$total += filesize($dir);
				unlink($fullpath);
			} else {
				delfile($fullpath);
			}
		}
	}
	closedir($dh);
	return rmdir($dir);
}